﻿/*
Customizer by Hoai Thanh Trinh
*/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using FShop.BAL;
using FShop.DAL;
public partial class ManageCategory : System.Web.UI.Page 
{      
    protected void Page_Load(object sender, EventArgs e)
    {
      
        if ( ! IsPostBack )
        {
           
            //ddlCategory.DataSource = Category_BAL.Get_CategoryAsTable("Cate_ParentID=0","");
            //ddlCategory.DataTextField = "Cate_Name";
            //ddlCategory.DataValueField = "Cate_ID";
            //ddlCategory.DataBind();
            BindDataGrid(0);
          			
        }   
        else
        {
			
        } 
    }
	#region Bind data to grid
    protected void BindDataGrid(int pageIndex)
    {
		try
		{
			      
            grdCategory.DataSource = Category_BAL.GetList_Category();        
			grdCategory.DataBind();				
		}
		catch(Exception ex)
        {
			throw ex;
        }
		
    } 
	#endregion		
	
	#region Delete Category
	protected void btnDeleteAll_Click(object sender, EventArgs e)
    {
		try{
			string s = CategorySelectedArray();
			if (string.IsNullOrEmpty(s)) return;
			Category_BAL.Delete_Category_Where("Cate_ID In ("+ s +")");
            MessageBox.Show("Đã xóa thành công.");
            BindDataGrid(1);
        }
        catch(Exception ex)
        {
            MessageBox.Show("Xóa không thành công. Bạn nên xóa hoặc di chuyển tất các tin tức thuộc danh mục cần xóa");
			//throw ex;
        }
    }
	//Lay ID tren cac cot duoc checked	
    private string CategorySelectedArray()
    {
        string strArrayID = "";
        foreach (GridViewRow rows in grdCategory.Rows)
        {
            CheckBox chk = (CheckBox)rows.FindControl("chkCheck");

            if (chk.Checked)
            {
                ImageButton btnDel = (ImageButton)rows.FindControl("btnDelete");
                if (strArrayID != "")
                {
                    strArrayID += ",";
                }
                strArrayID += btnDel.CommandArgument.Trim();
            }
        }
        return strArrayID;
    }
	
	#endregion
	
	#region Datagrid events
	protected void grdCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCategory.PageIndex = e.NewPageIndex;
        BindDataGrid(grdCategory.PageIndex);

    }
    protected void grdCategory_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string strCategoryID = e.CommandArgument.ToString().Trim();
        string strCommand = e.CommandName;                                   
        switch (strCommand)
        {             
            
            case "delete_Category":
                try
                {
					Category category = Category_BAL.Get_Category_ByID(int.Parse(strCategoryID));
                    if (category == null) return;
                    Category_BAL.Delete_Category(category);
                    MessageBox.Show("Đã xóa thành công.");
                    BindDataGrid(1);
                }
                catch(Exception ex)
				{
                    MessageBox.Show("Xóa không thành công. Bạn nên xóa hoặc di chuyển tất các tin tức thuộc danh mục cần xóa");
					//throw ex;
				}
                break;

            case "active_Category":
                try
                {
					Category category = Category_BAL.Get_Category_ByID(int.Parse(strCategoryID));
                    if (category == null) return;
                    category.Cate_Status = !category.Cate_Status;
                    Category_BAL.Update_Category(category);
                    MessageBox.Show("Đã kích hoạt thành công.");
                    BindDataGrid(1);
                }
                catch(Exception ex)
				{
					//throw ex;
                    MessageBox.Show("Lỗi :" + ex.Message);
				}
                break;   
        }
        
    }
	
	protected void grdCategory_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton imgActive = (ImageButton)e.Row.FindControl("btnActive");
            if (imgActive != null)
            {
                object obj = DataBinder.Eval(e.Row.DataItem, "Cate_Status");
                if (obj != null)
                {
                    if (obj.ToString().ToLower() == "true")
                    {
                        imgActive.ImageUrl = "Images/check.gif";
                    }
                    else
                    {
                        imgActive.ImageUrl = "Images/uncheck.gif";
                    }
                }
            }
        }
    }
	#endregion
	
	#region Extend events/codes
    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        if(ddlCategory.SelectedValue==null) return;
        grdCategory.DataSource = Category_BAL.Get_CategoryAsPaging(1, 15, "", "*", "Cate_ParentID="+ddlCategory.SelectedValue.ToString(), "");
        grdCategory.DataBind();	
    }
    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnSearch_Click(null, null);
    }
	#endregion



    protected void btnSearch_Click1(object sender, EventArgs e)
    {
        Response.Redirect("ManageCategory.aspx");
    }
}


